<template>
  <div class="import">
    <el-card>
      <h2 class="title">员工导入</h2>
      <upload-excel :before-upload="beforeUpload" :on-success="onSuccess" />
    </el-card>
  </div>
</template>

<script>
import { sysUserBatch } from '@/api/human'
import moment from 'moment'
export default {
  methods: {
    beforeUpload(file) {
      return true
    },
    async onSuccess(obj) {
      const temp = {
        入职时间: 'timeOfEntry',
        姓名: 'username',
        工号: 'workNumber',
        手机号: 'mobile',
        转正日期: 'correctionTime'
      }
      const newArr = obj.results.map((item) => {
        const newObj = {}
        Object.keys(item).forEach((item2) => {
          if (item2 === '入职日期' || item2 === '转正日期') {
            newObj[temp[item2]] = this.changeDate(item[item2])
          } else {
            newObj[temp[item2]] = item[item2]
          }
        })
        return newObj
      })
      await sysUserBatch(newArr)
      this.$message.success('导入成功')
      this.$router.push('/human')
      console.log(newArr)
    },
    changeDate(num) {
      const t = new Date((num - 1) * 24 * 60 * 60 * 1000 - 8 * 60 * 60 * 1000)
      t.setYear(t.getFullYear() - 70)
      return moment(t).format('YYYY-MM-DD')
    }
  }
}
</script>

<style lang="scss" scoped>
.import {
  padding: 20px;
  .title {
    text-align: center;
    margin-bottom: 30px;
  }
}
</style>
